﻿using Lhz.InternetService;
using Lhz.Sc.Business;
using Lhz.Sc.Entity.Database;
using Lhz.Sc.Entity.Enum;
using Lhz.Test.Common;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.Linq;

namespace Lhz.Sc.Test.LhzScService
{
    [TestClass]
    public class UnitTestAuthorization
    {
        private UserProxy uo = new UserProxy();
        private AuthorizationProxy ap = new AuthorizationProxy();
        private ServiceInvoker si = new ServiceInvoker(typeof(Authorization));

        [TestMethod]
        public void AuthorizationServiceTestMethodCommon()
        {
            var user = uo.Query().First();
            var authorization = TestEntityBuilder.BuildAuthorization(user);
            var dst = new ServiceCrudTester<Guid, Authorization>(TestConst.ServiceAuthorizationUrl);
            var changes = new Dictionary<string, object>();
            changes.Add("ObjectGuid", Guid.NewGuid());
            changes.Add("AuthorizationType", AuthorizationType.OperationAuthorization);
            dst.CrudTest(authorization, authorization.Guid, changes, new string[] { "User", "Application", "Service", "Operation" });
        }

        [TestMethod]
        public void AuthorizationServiceTestMethodQueryByPkWithNullPath()
        {
            var pks = ap.Query().Select(r => r.Guid).ToList();
            foreach (var pk in pks)
            {
                var a = si.Invoke<Authorization>(TestConst.ServiceAuthorizationUrl, "QueryByPk", pk, null);
                Assert.IsNotNull(a);
                Assert.IsNotNull(a.User);
                Assert.IsTrue(a.Application != null || a.Service != null || a.Operation != null);
            }
        }
    }
}
